/**
 * CategoryJdbcDao.java
 */
package com.babek.business.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.babek.business.dao.BaseDAO;
import com.babek.business.dao.CategoryDao;
import com.babek.business.entity.Category;

/**
 * @author aan
 * @Date Created Oct 1, 2011
 */

@Repository
public class CategoryJdbcDao extends JdbcDaoSupport implements CategoryDao {

	
	private DataSource dataSource;

	/**
	 * Constructor
	 */
	public CategoryJdbcDao() {
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.babek.business.dao.CategoryDao#getAllCategory()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Category> getAllParentCategory() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> rows = getJdbcTemplate().queryForList(
				"SELECT * From Category where parent_code=' '");
		List<Category> result = new ArrayList<Category>();

		for (Map row : rows) {
			Category cat = new Category();
			// Category
			// parentCat=getCategoryByCondition("parent_code",(String)row.get("parent_code"));
			List<Category> childCategory = getAllCategoryByCondition(
					"parent_code", (String) row.get("category_code"));

			cat.setCategoryId((Integer) row.get("category_id"));
			cat.setCategoryCode((String) row.get("category_code"));
			cat.setCategoryName((String) row.get("category_name"));
			cat.setCategoryUrl((String) row.get("category_url"));
			cat.setDateCreated((Date) row.get("date_created"));
			// cat.setParent(parentCat );
			cat.setChildCategory(childCategory);
			result.add(cat);
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.babek.business.dao.CategoryDao#getAllCategoryByCondition(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public List<Category> getAllCategoryByCondition(String condition, String val) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer().append(
				"SELECT * FROM Category where ").append(condition)
				.append(" = ").append(val);
		List<Category> result = getJdbcTemplate().query(sb.toString(),
				ParameterizedBeanPropertyRowMapper.newInstance(Category.class));
		return result;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.babek.business.dao.CategoryDao#getCategoryByCode(java.lang.String)
	 */
	@Override
	public Category getCategoryByCode(String categoryCode) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.babek.business.dao.CategoryDao#insertCategory(com.babek.business.
	 * entity.Category)
	 */
	@Override
	public void insertCategory(Category category) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.babek.business.dao.CategoryDao#ipdateCategory(com.babek.business.
	 * entity.Category)
	 */
	@Override
	public void updateCategory(Category category) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.babek.business.dao.CategoryDao#getCategoryByCondition(java.lang.String
	 * , java.lang.String)
	 */
	@Override
	public Category getCategoryByCondition(String condition, String val) {
		// TODO Auto-generated method stub
		Category cat = new Category();

		return null;
	}

	/* (non-Javadoc)
	 * @see com.babek.business.dao.CategoryDao#getAllCategories()
	 */
	@Override
	public List<Category> getAllCategories() {
		// TODO Auto-generated method stub
		return getJdbcTemplate().query("SELECT * FROM Category", ParameterizedBeanPropertyRowMapper.newInstance(Category.class));
	}

}
